/********************************** (C) COPYRIGHT
 ******************************** File Name          : main.c Author : WCH
 * Version            : V1.0.0
 * Date               : 2021/06/06
 * Description        : Main program body.
 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
 * SPDX-License-Identifier: Apache-2.0
 *******************************************************************************/
#include "lcd.h"
#include "lcd_init.h"
#include "tos_k.h"

//#include "tos_k.h"
#include "../../components/lvgl/examples/porting/lv_port_disp.h"
#include "../../components/lvgl/lvgl.h"
#include "../../components/lvgl/src/hal/lv_hal_tick.h"
#define LV_ATTRIBUTE_TICK_INC

int count = 0111;

static lv_obj_t *meter;

static void set_value(void *indic, int32_t v) {
    lv_meter_set_indicator_end_value(meter, indic, v);
}

/**
 * A meter with multiple arcs
 */
void lv_example_meter_xxxx(void) {
    meter = lv_meter_create(lv_scr_act());
    lv_obj_center(meter);
    lv_obj_set_size(meter, 200, 200);

    /*Remove the circle from the middle*/
    lv_obj_remove_style(meter, NULL, LV_PART_INDICATOR);

    /*Add a scale first*/
    lv_meter_scale_t *scale = lv_meter_add_scale(meter);
    lv_meter_set_scale_ticks(meter, scale, 11, 2, 10,
                             lv_palette_main(LV_PALETTE_GREY));
    lv_meter_set_scale_major_ticks(meter, scale, 1, 2, 30, lv_color_hex3(0xeee),
                                   10);
    lv_meter_set_scale_range(meter, scale, 0, 100, 270, 90);

    /*Add a three arc indicator*/
    lv_meter_indicator_t *indic1 =
        lv_meter_add_arc(meter, scale, 10, lv_palette_main(LV_PALETTE_RED), 0);
    lv_meter_indicator_t *indic2 = lv_meter_add_arc(
        meter, scale, 10, lv_palette_main(LV_PALETTE_GREEN), -10);
    lv_meter_indicator_t *indic3 = lv_meter_add_arc(
        meter, scale, 10, lv_palette_main(LV_PALETTE_BLUE), -20);

    /*Create an animation to set the value*/
    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_exec_cb(&a, set_value);
    lv_anim_set_values(&a, 0, 100);
    lv_anim_set_repeat_delay(&a, 100);
    lv_anim_set_playback_delay(&a, 100);
    lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);

    lv_anim_set_time(&a, 500);
    lv_anim_set_playback_time(&a, 200);
    lv_anim_set_var(&a, indic1);
    lv_anim_start(&a);

    lv_anim_set_time(&a, 300);
    lv_anim_set_playback_time(&a, 500);
    lv_anim_set_var(&a, indic2);
    lv_anim_start(&a);

    lv_anim_set_time(&a, 100);
    lv_anim_set_playback_time(&a, 1000);
    lv_anim_set_var(&a, indic3);
    lv_anim_start(&a);
}

extern void LCD_Init(void);
/*Initialize your display and the required peripherals.*/
static void disp_init(void) {
    /*You code here*/
    LCD_Init();
}

// extern void USART_Printf_Init(uint32_t baudrate);
extern void TIM1_PWMOut_Init(uint16_t arr, uint16_t psc, uint16_t ccp);

#define APPLICATION_TASK_STK_SIZE 8192
k_task_t application_task;
__aligned(4) uint8_t application_task_stk[APPLICATION_TASK_STK_SIZE];

void application_entry(void *arg) {
    int cnt = 0;

    uint8_t i;
    for (i = 0; i < 8; i++) {
        tos_task_delay(50);
    }

    lv_init();
    disp_init();
    lv_port_disp_init();

    lv_example_meter_xxxx();
    printf("lvgl demo start!\r\n");

    //LcdTask();
    while (1) {
        lv_task_handler();
        tos_task_delay(5);

        cnt++;
        if (cnt == 200) {
            cnt = 0;
            printf("lvgl task running!\r\n");
        }
    };
}

/*********************************************************************
 * @fn      main
 *
 * @brief   Main program.
 *
 * @return  none
 */

extern void board_led_key_init();
int main(void) {
    USART_Printf_Init(115200);
    board_led_key_init();
    TIM6_Init(100 - 1, 144 - 1);
    Interrupt_Init();
    TIM1_PWMOut_Init(40 - 1, 144 - 1, 5);
    TIM4_PWMOut_Init(40 - 1, 144 - 1, 10);
    SPI2_Init();
    tos_knl_init(); // 初始化TencentOS tiny内核

    printf("TinyOS demo1!\r\n");

    // 创建一个优先级为5的任务
    tos_task_create(&application_task, "task_prio5", application_entry, NULL, 4,
                    application_task_stk, APPLICATION_TASK_STK_SIZE, 0);
    // 开始内核调度，线程中不允许有睡死代码。
    tos_knl_start();

    while (1) {
        /* code */
    }
}
